package com.aifast.common.base;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.StrUtil;
import com.aifast.common.properties.Setting;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import com.aifast.common.global.Status;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.*;

/**
 *  封装常用方法 只是为了减少写代码
 *
 */
//跨域
@CrossOrigin
public abstract class Controller{

    @Autowired
    protected HttpServletRequest request;
    @Autowired
    protected HttpServletResponse response;
    @Autowired
    protected Setting setting;

    /**
     * 获取参数值
     * @param name 参数名
     * @return
     */
    public String get(String name){
        String parameter = request.getParameter(name);
        return StrUtil.isNotBlank(parameter) ? parameter : null;
    }

    /**
     * 获取参数值 如果获取不到则返回自定义的默认值
     * @param name 参数名
     * @param defaultValue 自定义默认值
     * @return
     */
    public String get(String name,String defaultValue){
        String parameter = request.getParameter(name);
        return StrUtil.isNotBlank(parameter) ? parameter : defaultValue;
    }

    public Integer getInt(String name){
        return Convert.toInt(get(name));
    }

    public Long getLong(String name){
        return Convert.toLong(get(name));
    }

    public Double getDouble(String name){
        return Convert.toDouble(get(name));
    }

    public Boolean getBoolean(String name){
        return Convert.toBool(get(name));
    }

    public Date getDate(String name){
        return Convert.toDate(get(name));
    }

    public LocalDateTime getLocalDate(String name){
        return Convert.toLocalDateTime(get(name));
    }

    public Integer getInt(String name,String defaultValue){
        return Convert.toInt(get(name,defaultValue));
    }

    public Long getLong(String name,String defaultValue){
        return Convert.toLong(get(name,defaultValue));
    }

    public Double getDouble(String name,String defaultValue){
        return Convert.toDouble(get(name,defaultValue));
    }

    public Boolean getBoolean(String name,String defaultValue){
        return Convert.toBool(get(name,defaultValue));
    }

    public Date getDate(String name,String defaultValue){
        return Convert.toDate(get(name,defaultValue));
    }

    public LocalDateTime getLocalDate(String name,String defaultValue){
        return Convert.toLocalDateTime(get(name,defaultValue));
    }

    public String[] getStrArray(String name){
        return Convert.toStrArray(get(name));
    }

    /**
     * 获取关键字参数
     * @return
     */
    public String getSearchValue(){
        String search = get("search");
        String searchValue = get("searchValue");
        return StrUtil.isNotBlank(search) ? search : searchValue;
    }

    /**
     * 自动获取分页参数
     * 这里默认是layui的datatable中的page和limit
     * @return
     */
    public Page getPage(){
        return new Page(Opt.ofNullable(getInt("page")).orElse(1), Opt.ofNullable(getInt("limit")).orElse(10));
    }

    /**
     * 自定义获取分页参数
     *
     * @param page     page参数名
     * @param pageSize pageSize参数名
     * @return
     */
    public Page getPage(String page, String pageSize) {
        return new Page(getInt(page), getInt(pageSize));
    }

    public Page getPage(Integer page, Integer pageSize) {
        return new Page(page, pageSize);
    }

    /**
     * 获取全部参数的map形式
     *
     * @return
     */
    public Map<String, Object> getMap() {
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> env = new HashMap<>();
        map.forEach((k, v) -> {
            String[] strings = map.get(k);
            String tmp = (!Objects.equals(null,strings) && strings.length > 0) ? strings[0] : null;
            env.put(k,tmp);
        });
        return env;
    }

    public <T> T getBean(Class<T> clazz){
        return BeanUtil.toBean(getMap(),clazz);
    }

    public Enumeration<String> getParaNames(){
        return request.getParameterNames();
    }


    /**
     * 封装ajax请求成功返回格式
     * @return
     */
    protected static R OK(){
        return R.OK();
    }

    /**
     * 封装ajax请求成功返回格式
     * @param o 数据
     * @return
     */
    protected static R OK(Object o){
        return R.OK(o);
    }

    /**
     * 封装ajax请求错误返回格式
     * @return
     */
    protected static R ERROR(){
        return R.ERROR();
    }

    /**
     * 封装ajax请求错误返回格式
     * @param s 错误提示
     * @return
     */
    protected static R ERROR(String s){
        return R.ERROR(s);
    }

    /**
     * 封装ajax请求返回格式
     * @param status 自定状态
     * @return
     */
    protected static R OK(Status status){
        return R.ERROR(status);
    }

}
